package top.roadvast.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.junit.Test;
import top.roadvast.activiti.pojo.Person;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 郭泽鹏
 * @Date 2022/3/20 16:23
 * @Version 1.0
 * @Description 获取流程变量
 */
public class SetOrGetProdVariable {
    final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();		//获取任务的Service，设置和获取流程变量
    private String taskId = "5002";
    private String assignee = "zhangsan";

    @Test
    public void setVariableByRt() {
        final RuntimeService runtimeService = processEngine.getRuntimeService();
        final List<ProcessInstance> list = runtimeService
                .createProcessInstanceQuery()
                .processDefinitionId("holiday-group:1:4")
                .list();
        Map<String, String> map = new HashMap<>();
        map.put("assigners0", "lilei,hanmeimei");
        for (ProcessInstance processInstance : list) {
            runtimeService.setVariables(processInstance.getId(), map);
        }

    }

    public Task getTask() {
        //查询当前办理人的任务ID
        return taskService.createTaskQuery()
                .taskId(taskId)
                .singleResult();
    }

    //设置基本类型的流程变量
    @Test
    public void setBasicVariables01(){
        final Task task = getTask();

        //设置流程变量【基本类型】
//        taskService.setVariable(task.getId(), "请假人", assignee);
//        taskService.setVariableLocal(task.getId(), "请假天数",3);
//        taskService.setVariable(task.getId(), "请假日期", new Date());
        taskService.setVariable(task.getId(), "assigners0", "lilei,hanmeimei");
    }

    //设置基本类型的流程变量
    @Test
    public void setBasicVariables(){
        final Task task = getTask();

        //设置流程变量【基本类型】
        taskService.setVariable(task.getId(), "请假人", assignee);
        taskService.setVariableLocal(task.getId(), "请假天数",3);
        taskService.setVariable(task.getId(), "请假日期", new Date());
    }

    //设置java bean类型的变量
    @Test
    public void setJavaBeanVariables(){
        final Task task = getTask();

        //设置流程变量【javabean类型】
        Person p = new Person();
        p.setId(1);
        p.setName(assignee);
        taskService.setVariable(task.getId(), "人员信息", p);
        System.out.println("流程变量设置成功~~");
    }

    //获取基本类型的流程变量
    @Test
    public void getBasicVariables() {
        final Task task = getTask();
        System.out.println("请假人：" + taskService.getVariable(task.getId(), "请假人"));
        System.out.println("请假天数：" + taskService.getVariable(task.getId(), "请假天数"));
        System.out.println("请假日期：" + taskService.getVariable(task.getId(), "请假日期"));
    }

    //获取javaBean流程变量
    @Test
    public void getObjVariables() {
        final Task task = getTask();
        System.out.println((Person)taskService.getVariable(task.getId(), "人员信息"));
    }

    //获取历史流程变量
    @Test
    public void getHistoryProcessVariables(){
        List<HistoricVariableInstance> list = processEngine.getHistoryService()
                .createHistoricVariableInstanceQuery()      //创建一个历史的流程变量查询
                .variableName("请假天数")
                .list();

        if(list != null && list.size() > 0) {
            for(HistoricVariableInstance hiv : list) {
                System.out.println(hiv.getTaskId()+"  "+hiv.getVariableName()+"		"+hiv.getValue()+"		"+hiv.getVariableTypeName());
            }
        }
    }
}
